package observer;

import observer.adapter.impl.InvestAdapter;
import observer.adapter.impl.Investor;
import observer.invest.impl.Gold;
import observer.invest.impl.Liquor;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class Main {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        Gold gold = new Gold();
        Liquor liquor = new Liquor();
        Investor 小王 = new Investor("小王");
        Investor 小明 = new Investor("小明");
        InvestAdapter observer = new InvestAdapter(小明);
        InvestAdapter observer1 = new InvestAdapter(小王);
        gold.attach(observer);
        gold.attach(observer1);
        liquor.attach(observer1);

        Timer timer = new Timer();
        Random random = new Random(System.currentTimeMillis());
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                int i = 0;
                if (liquor.status != 0) {
                    i = random.nextInt(20);
                    liquor.realTimeValue(i);
                }
                if (gold.status != 0) {
                    i = random.nextInt(10);
                    gold.realTimeValue(i);
                }
                if (gold.status == 0 && liquor.status == 0) {
                    timer.cancel();
                }
            }
        }, 1000, 1000);

    }
}
